package dnl.dev.memlog;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;

import javax.swing.AbstractAction;
import javax.swing.JPopupMenu;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

import org.apache.commons.io.FileUtils;

import dnl.util.ui.PopupUtils;


/**
 * 
 * @author daniel
 */
public class SnapshotsView extends View {

	private MemlogContext memlogContext;

	/** Creates new form SnapshotsView2 */
	public SnapshotsView() {
		super("Snapshots");
		initComponents();
		ListSelectionModel selectionModel = snapshotsTable.getSelectionModel();
		addListSelectionListener(selectionModel);
		JPopupMenu popupMenu = new JPopupMenu();
		popupMenu.add(new AbstractAction("Delete") {
			@Override
			public void actionPerformed(ActionEvent e) {
				String selectedSnapshotName = getSelectedSnapshotName();
				memlogContext.deleteSnapshot(selectedSnapshotName);
				rePresent();
			}
		});
		PopupUtils.addPopup(snapshotsTable, popupMenu);
	}

	private void addListSelectionListener(ListSelectionModel selectionModel) {
		selectionModel.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				String fname = getSelectedSnapshotName();
				if (fname == null) {
					return;
				}
				try {
					String s = FileUtils.readFileToString(memlogContext.getHistogramsDir()
							.getHistogramFile(fname));
					snapshotTP.setText(s);
					SwingUtilities.invokeLater(new Runnable() {
						@Override
						public void run() {
							tpScrollPane.getViewport().setViewPosition(new Point(0, 0));
						}
					});
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}

		});
	}

	private String getSelectedSnapshotName() {
		int selectedRow = snapshotsTable.getSelectedRow();
		String fname = (String) snapshotsTable.getModel().getValueAt(selectedRow, 0);
		return fname;
	}

	@Override
	public void present(MemlogContext memlogContext) {
		this.memlogContext = memlogContext;
		snapshotsTable.setModel(new TM());
		snapshotTP.setText("");
	}

	protected void rePresent() {
		present(memlogContext);
	}

	private class TM extends AbstractTableModel {

		String[] fileNames;

		public TM() {
			fileNames = memlogContext.getHistogramsDir().listHistogramFileNames();
			Arrays.sort(fileNames, new Comparator<String>() {
				@Override
				public int compare(String o1, String o2) {
					Date d1 = memlogContext.getFileCreationDate(o1);
					Date d2 = memlogContext.getFileCreationDate(o2);
					return d1.compareTo(d2);
				}
			});
		}

		@Override
		public String getColumnName(int column) {
			if (column == 0)
				return "Name";
			return "Time";
		}

		@Override
		public int getColumnCount() {
			return 2;
		}

		@Override
		public int getRowCount() {
			return memlogContext.getNumberOfHistograms();
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			if (rowIndex < 0 || rowIndex >= fileNames.length) {
				return null;
			}
			if (columnIndex == 0) {
				return fileNames[rowIndex];
			}
			if (columnIndex == 1) {
				return memlogContext.getMemlogProject().getFileCreationDate(fileNames[rowIndex]);
			}
			return null;
		}

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jSplitPane1 = new javax.swing.JSplitPane();
		jPanel1 = new javax.swing.JPanel();
		tpScrollPane = new javax.swing.JScrollPane();
		snapshotTP = new javax.swing.JTextPane();
		jPanel2 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		snapshotsTable = new javax.swing.JTable();

		setLayout(new java.awt.BorderLayout());

		jSplitPane1.setDividerLocation(400);

		jPanel1.setLayout(new java.awt.BorderLayout());

		tpScrollPane.setViewportView(snapshotTP);

		jPanel1.add(tpScrollPane, java.awt.BorderLayout.CENTER);

		jLabel1.setText("Snapshot File Content:");
		jPanel2.add(jLabel1);

		jPanel1.add(jPanel2, java.awt.BorderLayout.NORTH);

		jSplitPane1.setRightComponent(jPanel1);

		jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Snapshots"));

		snapshotsTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { {}, {},
				{}, {} }, new String[] {

		}));
		jScrollPane1.setViewportView(snapshotsTable);

		jSplitPane1.setLeftComponent(jScrollPane1);

		add(jSplitPane1, java.awt.BorderLayout.CENTER);
	}// </editor-fold>//GEN-END:initComponents

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JLabel jLabel1;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JSplitPane jSplitPane1;
	private javax.swing.JTextPane snapshotTP;
	private javax.swing.JTable snapshotsTable;
	private javax.swing.JScrollPane tpScrollPane;
	// End of variables declaration//GEN-END:variables

}
